package com.ld.security.auth.common.base.service.impl;

import com.ld.security.auth.common.base.constants.JwtFunctionConstant;
import com.ld.security.auth.common.base.entity.JwtPermission;
import com.ld.security.auth.common.base.service.InitJwtPermissionService;
import com.ld.security.auth.common.feign.TreeFeign;
import com.ld.security.common.core.anno.QueryMapping;
import com.ld.security.common.core.base.column.ListString;
import com.ld.security.common.core.base.po.EntitysPo;
import com.ld.security.common.core.util.TreeUtil;
import com.ld.security.common.core.util.UuidUtil;
import com.ld.security.common.mybatis.base.entity.TreeEntity;
import com.ld.security.common.tomcat.base.api.SuperApi;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.autoconfigure.condition.ConditionalOnClass;
import org.springframework.core.annotation.AnnotationUtils;
import org.springframework.stereotype.Component;
import org.springframework.web.bind.annotation.RequestMapping;

import java.lang.reflect.Method;
import java.util.Arrays;
import java.util.LinkedList;
import java.util.List;
import java.util.stream.Collectors;

/**
 * 角色权限初始化实现
 */
@Component
@ConditionalOnClass
class InitJwtPermissionServiceImpl extends InitAppServie<JwtPermission> implements InitJwtPermissionService {

    @Autowired(required = false)
    private List<? extends SuperApi> apiList;

    @Autowired
    private TreeFeign treeFeign;



    @Override
    public JwtPermission newJwtPermission(Class<?> clazz) {
          JwtPermission root =  this.getRoot();

        JwtPermission jwtPermissionGroup =  new JwtPermission();
        Api api =  AnnotationUtils.findAnnotation(clazz,Api.class);
        RequestMapping parentMapping =  AnnotationUtils.findAnnotation(clazz,RequestMapping.class);
        jwtPermissionGroup.setLabel(api.tags()[0]);
        jwtPermissionGroup.setUrl(root.getUrl()+ parentMapping.value()[0]);
        jwtPermissionGroup.setParentPath(new ListString(TreeEntity.root));
        jwtPermissionGroup.setId(UuidUtil.getId(jwtPermissionGroup.getUrl()));
        jwtPermissionGroup.setFuncType(JwtFunctionConstant.funcType.api);

        List<JwtPermission> jwtPermissions = this.buildJwtPermission(jwtPermissionGroup,clazz);

        jwtPermissionGroup.setChildren(jwtPermissions);
        return jwtPermissionGroup;
    }


    public List<JwtPermission> buildJwtPermission(JwtPermission root, Class<?> clazz) {
        LinkedList<JwtPermission> list =  new LinkedList<>();

        for (Method method : clazz.getMethods()) {
            JwtPermission jwtPermission = new JwtPermission();
            QueryMapping queryMapping =  AnnotationUtils.findAnnotation(method,QueryMapping.class);

           if(queryMapping == null){
                continue;
            }
            RequestMapping methodMapping =  AnnotationUtils.findAnnotation(method,RequestMapping.class);
            ApiOperation apiOperation =  AnnotationUtils.findAnnotation(method,ApiOperation.class);

            String label = apiOperation.value();
            String value =methodMapping.value()[0];

            jwtPermission.setFuncType(JwtFunctionConstant.getFuncType(value));
            jwtPermission.setLabel(label);
            jwtPermission.setUrl(root.getUrl()+value);
            jwtPermission.setId(UuidUtil.getId( jwtPermission.getUrl()));
            list.add(jwtPermission);
        }
        return list;
    }

    @Override
    public void initSystemPermission() {

        JwtPermission root =this.getRoot();
        List<JwtPermission> list =  apiList.stream().map(val -> {
             return this.newJwtPermission(val.getClass());
         }).collect(Collectors.toList());
        root.setChildren(list);
        TreeUtil.buildTreePath(root);
        EntitysPo po =   new EntitysPo<>();
        po.setList(Arrays.asList(root));
        treeFeign.initPermission(po);
    }

    @Override
    public void initSystem() {
        this.initSystemPermission();
    }
}
